package com.training.hatton.dropboxclient;

import java.io.File;

import java.util.concurrent.TimeUnit;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.training.hatton.dropboxclient.content.contract.MetadataContract;
import com.training.hatton.dropboxclient.fragment.DropboxListFragment;
import com.training.hatton.dropboxclient.service.SyncService;
import com.training.hatton.dropboxclient.utill.DropboxService;

public class DropboxContainerActivity extends FragmentActivity{

	private String mPath;

	public String getPath() {
		return mPath;
	}

	public void setPath(String path) { 
		this.mPath = path;
		}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dropbox_container);
		if(getSupportFragmentManager().getBackStackEntryCount() == 0){
			setFirstFragment();
		}
		Intent intent = new Intent("start", null, this, SyncService.class);
		startService(intent);
	}

	private void setFirstFragment() {
		DropboxListFragment listFragment = new DropboxListFragment();
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.fragment_container, listFragment);
		ft.addToBackStack(null);
		ft.commit();
	}

	public void onRefresh(View view) {
		getContentResolver().delete(MetadataContract.MetadataColumns.CONTENT_URI, null, null);
		File[] files = ContextHolder.getInstance().getContext().getCacheDir().listFiles();
		for (File file : files) {
			file.delete();
		}
	}
	
	public void onSignOut(View view) {
		DropboxService.logOut();
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		startActivity(new Intent(this, DropboxLauncherActivity.class));
		finish();
	}
}
